<?php

if( file_exists("../lib/mysql_connect.php") )
  require("../lib/mysql_connect.php");
else
  require("../../lib/mysql_connect.php");

function get_details_atelier($id_atelier)
{
  $ateliers_req = "SELECT * FROM atelier,consigne_securite WHERE
                   atelier.id_atelier = '".$id_atelier."' AND
                   atelier.id_atelier = consigne_securite.id_atelier";

  $ateliers_result = mysql_query($ateliers_req);

  if( $ateliers_result != FALSE )
  {
    $atelier = mysql_fetch_array($ateliers_result);
    $temp_array = array(   'nom'          => $atelier['nom'], 
                           'description'  => $atelier['description'],
                           'consigne'     => $atelier['consigne']);
    return $temp_array;
  }
  else
    return NULL;
}

function get_muscles_atelier($id_atelier)
{
  $muscle_req = "SELECT * FROM muscle_solicite,muscle WHERE
                   muscle_solicite.id_atelier = '".$id_atelier."' AND
                   muscle_solicite.id_muscle = muscle.id_muscle";

  $groupe_req = "SELECT * FROM groupe_muscle_solicite,groupe_musculaire WHERE
                   groupe_muscle_solicite.id_atelier = '".$id_atelier."' AND
                   groupe_muscle_solicite.id_groupe_musculaire = groupe_musculaire.id_groupe_musculaire";

  $muscle_result = mysql_query($muscle_req);
  $groupe_result = mysql_query($groupe_req);

  if( $muscle_result != FALSE && $groupe_result != FALSE )
  {
    $result = array( 'groupes' => array(), 'muscles' => array() );
    while( $muscle = mysql_fetch_array($muscle_result) )
    {
      $temp_array = array( 'nom_muscle' => $muscle['muscle'] ); 
      array_push( $result['muscles'], $temp_array );
    }

    while( $groupe = mysql_fetch_array($groupe_result) )
    {
      $temp_array = array( 'nom_groupe' => $groupe['groupe_musculaire'] ); 
      array_push( $result['groupes'], $temp_array );
    }

    return $result;
  }
  else
    return NULL;
}

/*************************************
 * Traitement AJAX                   *
 * Voir la fiche d'un atelier        *
 * **********************************/

if( isset($_POST['voir_fiche']) && $_POST['voir_fiche'] == 1 )
{
  $id_atelier = $_POST['id_atelier'];
  $detail_atelier = get_details_atelier($id_atelier);
  $muscles = get_muscles_atelier($id_atelier);
  
  echo "<div class='contenu_haut'>.: Détails atelier :.</div><div class='contenu_fond'>";
  echo "<div id='details_fiche_atelier'>";
  echo "<table>
          <tr>
            <td><h3>Nom : </h3></td>
            <td>".$detail_atelier['nom']."</td>
          </tr>
          <tr>
            <td><h3>Description : </h3></td>
            <td>".$detail_atelier['description']."</td>
          </tr>
          <tr>
            <td><h3>Consignes : </h3></td>
            <td>".$detail_atelier['consigne']."</td>
          </tr>
        </table>";
  echo "</div>";
  echo "</div><div class='contenu_bas'></div>";


  echo "<div class='contenu_haut'>.: Muscles travaillés :.</div><div class='contenu_fond'>";

  if( count($muscles['muscles']) == 0 && count($muscles['groupes']) == 0 )
    echo "<div class='erreur_div'>Aucun muscle déclaré pour cet atelier</div>";
  else
  {
    echo "<div id='muscles_atelier'>";
    echo "<table>
            <tr>
              <td> <h3>Groupes musculaires solicités :</h3> </td>
              <td><ul>";

    for($i = 0; $i<count($muscles['groupes']); $i++)
      echo "<li>".$muscles['groupes'][$i]['nom_groupe']."</li>";

    echo "</ul></td>";
    echo "</tr>
            <td> <h3>Muscles solicités :</h3> </td>
            <td><ul>";
    
    for($i = 0; $i<count($muscles['muscles']); $i++)
      echo "<li>".$muscles['muscles'][$i]['nom_muscle']."</li>";

    echo "</ul></td>";
    echo "</tr>";
    echo "</table>";
    echo "</div>";
  }

  echo "</div><div class='contenu_bas'></div>";

}


?>
